package com.ee.chat.common.server.mq;

import com.aliyun.openservices.ons.api.MessageListener;
import com.aliyun.openservices.ons.api.PropertyKeyConst;
import com.aliyun.openservices.ons.api.bean.*;
import com.aliyun.openservices.ons.api.order.MessageOrderListener;
import com.ee.chat.common.serconfig.MyMQConfig;
import com.ndhutu.ee.util.StringUtil;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

/**
 * Rocket MQ消息队列服务类
 */
@Configuration
public class MQServer {
    @Resource
    MyMQConfig mqConfig;

    /**
     * 普通消息生产者
     * @return
     */
    @Bean(initMethod = "start", destroyMethod = "shutdown")
    public ProducerBean buildProducer() {
        ProducerBean producer = new ProducerBean();
        producer.setProperties(mqConfig.getMqPropertie());
        return producer;
    }

    /**
     * 顺序消息生产者
     * @return
     */
    @Bean(initMethod = "start", destroyMethod = "shutdown")
    public OrderProducerBean buildOrderProducer() {
        OrderProducerBean orderProducerBean = new OrderProducerBean();
        orderProducerBean.setProperties(mqConfig.getMqPropertie());
        return orderProducerBean;
    }

    /**
     * 普通消息消费者
     * @param groupId
     * @param messageModel  订阅方式，PropertyValueConst.CLUSTERING-集群订阅（默认）  PropertyValueConst.BROADCASTING-广播订阅
     * @param messages  消息列表，包含
     *                  topic 消息主题，一级消息类型，
     *                  tag   消息标签，二级消息类型
     *                  messageListener 消息消费监听
     * @return
     */
    public ConsumerBean buildConsumer(String groupId, String messageModel, List<Map<String, Object>> messages) {
        ConsumerBean consumerBean = new ConsumerBean();
        //配置文件
        Properties properties = mqConfig.getMqPropertie();
        properties.setProperty(PropertyKeyConst.GROUP_ID, groupId);
        //将消费者线程数固定为20个 20为默认值
        properties.setProperty(PropertyKeyConst.ConsumeThreadNums, "20");
        if(!StringUtil.isEmpty(messageModel)){
            properties.setProperty(PropertyKeyConst.MessageModel, messageModel);
        }
        consumerBean.setProperties(properties);

        //订阅关系
        Map<Subscription, MessageListener> subscriptionTable = new HashMap<>();
        for(Map<String, Object> message : messages){
            Subscription subscription = new Subscription();
            String topic = message.get("topic")+"";
            subscription.setTopic(topic);

            String tag = message.get("tag")+"";
            if(!StringUtil.isEmpty(tag)){
                subscription.setExpression(tag);
            }

            MessageListener messageListener = (MessageListener) message.get("messageListener");
            subscriptionTable.put(subscription, messageListener);
        }

        consumerBean.setSubscriptionTable(subscriptionTable);
        return consumerBean;
    }

    /**
     * 顺序消息消费者
     * @param groupId
     * @param messageModel  订阅方式，PropertyValueConst.CLUSTERING-集群订阅（默认）  PropertyValueConst.BROADCASTING-广播订阅
     * @param messages  消息列表，包含
     *                  topic 消息主题，一级消息类型，
     *                  tag   消息标签，二级消息类型
     *                  messageOrderListener 消息消费监听
     * @return
     */
    public OrderConsumerBean buildOrderConsumer(String groupId, String messageModel, List<Map<String, Object>> messages) {
        OrderConsumerBean orderConsumerBean = new OrderConsumerBean();
        //配置文件
        Properties properties = mqConfig.getMqPropertie();
        properties.setProperty(PropertyKeyConst.GROUP_ID, groupId);
        //将消费者线程数固定为20个 20为默认值
        properties.setProperty(PropertyKeyConst.ConsumeThreadNums, "20");
        if(!StringUtil.isEmpty(messageModel)){
            properties.setProperty(PropertyKeyConst.MessageModel, messageModel);
        }
        orderConsumerBean.setProperties(properties);

        //订阅关系
        Map<Subscription, MessageOrderListener> subscriptionTable = new HashMap<>();
        for(Map<String, Object> message : messages){
            Subscription subscription = new Subscription();
            String topic = message.get("topic")+"";
            subscription.setTopic(topic);

            String tag = message.get("tag")+"";
            if(!StringUtil.isEmpty(tag)){
                subscription.setExpression(tag);
            }

            MessageOrderListener messageListener = (MessageOrderListener) message.get("messageListener");
            subscriptionTable.put(subscription, messageListener);
        }

        orderConsumerBean.setSubscriptionTable(subscriptionTable);
        return orderConsumerBean;
    }


}
